<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/2/12
 * Time: 8:57 PM
 */

namespace backend\models;


class Mission extends \common\models\Mission
{

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
//            [['uid', 'type'], 'required'],
            [
                [
                    'uid',
                    'type',
                    'status',
                    'forbidden',
                    'pay_needed',
                    'created_at',
                    'updated_at',
                    'mission_type',
                    'gender_needed',
                    'salary',
                    'pay_type',
                    'view_num',
                ],
                'integer'
            ],
            [
                [
                    'content',
                    'start_at',
                    'end_at',
                ],
                'string'
            ],
            [
                [
                    'title',
                    'contact_name',
                    'contact_phone',
                    'latitude',
                    'longitude',
                    'ad_code',
                ],
                'string',
                'max' => 255
            ],
//            [
//                ['uid'],
//                'exist',
//                'skipOnError'     => true,
//                'targetClass'     => User::className(),
//                'targetAttribute' => ['uid' => 'id']
//            ],
            [['work_start', 'work_end'], 'safe'],
            ['end_at', 'validateEndTime'],
            ['work_end', 'validateWorkTime'],
        ];
    }

    public function validateEndTime($attribute, $params)
    {
        if ( ! $this->hasErrors()) {
            if ($this->start_at > $this->end_at) {
                $this->addError($attribute, '结束时间不能早于开始时间');
            }
        }
    }

    public function validateWorkTime($attribute, $params)
    {
        if ( ! $this->hasErrors()) {
            if ($this->work_start > $this->work_end) {
                $this->addError($attribute, '下班时间不能早于上班时间');
            }
        }
    }


    public function beforeSave($insert)
    {
        $this->type     = self::MISSION_TYPE;
        $this->uid      = isset($this->uid) ? $this->uid : 1;
        $this->start_at = strtotime($this->start_at);
        $this->end_at   = strtotime($this->end_at);
        if ($insert) {
            $this->created_at = $this->updated_at = time();
        } else {
            $this->created_at = time();
        }

        return parent::beforeSave($insert);
    }


    public function afterSave($insert, $changedAttributes)
    {
        $info = new JobInfo();
//        var_dump($info);
        if ($insert) {
            $info->jid = $this->id;
        } else {
            $info = JobInfo::findOne(['jid' => $this->id]);
            if (empty($info)) {
                $info      = new JobInfo();
                $info->jid = $this->id;
            }
        }

        $info->mission_type  = empty($this->mission_type) ? 1 : $this->mission_type;
        $info->latitude      = $this->latitude;
        $info->longitude     = $this->longitude;
        $info->ad_code       = $this->ad_code;
        $info->gender_needed = $this->gender_needed;
        $info->start_at      = $this->start_at;
        $info->end_at        = $this->end_at;
        $info->work_end      = $this->work_end;
        $info->work_start    = $this->work_start;
        $info->salary        = $this->salary;
        $info->pay_typed     = $this->pay_type;
        $info->save(false);

        parent::afterSave($insert, $changedAttributes);
    }

    public function afterFind()
    {
        $this->pay_type = isset($this->pay_type) ? $this->pay_type : 1;
        parent::afterFind(); // TODO: Change the autogenerated stub
    }

    public function beforeDelete()
    {
        if ($this->jobInfo) {
            $this->jobInfo->delete();
        }
        if ($this->jobRecords) {
            foreach ($this->jobRecords as $record) {
                $record->delete();
            }
        }


        return parent::beforeDelete(); // TODO: Change the autogenerated stub
    }

}